//
//  BerizeHeader.m
//  BaxTer
//
//  Created by wsj2015 on 2016/11/21.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BerizeHeader.h"


#define headerH 160
@implementation BerizeHeader
{
    CAShapeLayer *_shapeLayer;
}

- (instancetype)head{
  
    BerizeHeader *head =[[BerizeHeader alloc] init];
    CGFloat f = kScreenFactor;
    head.frame = CGRectMake(0, 0, kScreenWidth, headerH *f);

    return head;
}

- (void)drawRect:(CGRect)rect {
 
}
-(void)showAnimtionWithColor:(UIColor *)color
{
    
    
    
    if (_shapeLayer) {
        [_shapeLayer removeAllAnimations];
        [_shapeLayer removeFromSuperlayer];
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    _shapeLayer = shapeLayer;
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(-1, 65)];
    [path addLineToPoint:CGPointMake(-1,-1)];
   
    [path addLineToPoint:CGPointMake(kScreenWidth +2, -1)];
    [path addLineToPoint:CGPointMake(kScreenWidth+2, 65)];
 
   
    
    CGFloat f = kScreenFactor;
    [path addQuadCurveToPoint:CGPointMake(-1, 65) controlPoint:CGPointMake(kScreenWidth/2, (headerH + 30)*f)];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = color.CGColor;
    shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
    shapeLayer.lineWidth = 1;
    [self.layer insertSublayer:shapeLayer atIndex:0];
    CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
    morph.duration = .5;
    
    UIBezierPath *originPath = [UIBezierPath bezierPath];
    [originPath moveToPoint:CGPointMake(-1, 65)];
    [originPath addLineToPoint:CGPointMake(-1,-1)];
 
    morph.toValue =  (__bridge id _Nullable)(path.CGPath);
    morph.fromValue = (__bridge id _Nullable)(originPath.CGPath);
    //移动后位置保持结束后的状态
    morph.fillMode=kCAFillModeForwards;
    morph.removedOnCompletion =NO;
   ;
    //morph.delegate = self;

    [shapeLayer addAnimation:morph forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if(self.animationStop){
    self.animationStop();
    }
    
}

@end
